/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.launching;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class ActionPerformer {

	private ItemTypeResolver itemTypeResolver;
	
	public ActionPerformer() {

		itemTypeResolver = new ItemTypeResolver();
	}
	
	public boolean performActionFor(String command) {
		
		ItemType type = itemTypeResolver.determineTypeOf(command);
		
		if(type == ItemType.FILE) {
			performActionForFile(command);
			return true;
		} else if(type == ItemType.URI) {
			performActionForURI(command);
			return true;
		}
		
		return false;
	}

	private void performActionForFile(String path) {
		
		File file = new File(path);
		
		if(file.exists()) {
			
			FileExecutor fileExecutor = new FileExecutor(file);
			if(file.canExecute()) 
				fileExecutor.execute();
			else if(file.isFile())
			    fileExecutor.execute();
			else if(file.isDirectory())
			    fileExecutor.execute();
		} else {

		}	
	}
	
	private void performActionForURI(String URI) {
		
		try {
			URI uri = new java.net.URI(URI);
			URIExecutor uriExecutor = new URIExecutor(uri);
			uriExecutor.execute();
			
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		
	}
	
}
